<template><div><h2 id="laravel-envoy" tabindex="-1"><a class="header-anchor" href="#laravel-envoy"><span>Laravel Envoy</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#installation">安装</a></li>
<li><a href="#writing-tasks">编写任务</a>
<ul>
<li><a href="#defining-tasks">定义任务</a></li>
<li><a href="#multiple-servers">多服务器</a></li>
<li><a href="#setup">配置</a></li>
<li><a href="#variables">变量</a></li>
<li><a href="#stories">脚本故事</a></li>
<li><a href="#completion-hooks">任务钩子</a></li>
</ul>
</li>
<li><a href="#running-tasks">运行任务</a>
<ul>
<li><a href="#confirming-task-execution">任务确认</a></li>
</ul>
</li>
<li><a href="#notifications">消息通知</a>
<ul>
<li><a href="#slack">Slack</a></li>
<li><a href="#discord">Discord</a></li>
<li><a href="#telegram">Telegram</a></li>
<li><a href="#microsoft-teams">Microsoft Teams</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p><a href="https://github.com/laravel/envoy" target="_blank" rel="noopener noreferrer">Laravel Envoy</a> 是一套在远程服务器上执行日常任务的工具。 使用 <a href="https://learnku.com/docs/laravel/10.x/blade" target="_blank" rel="noopener noreferrer">Blade</a> 风格语法，你可以轻松地配置部署任务、Artisan 命令的执行等。目前， Envoy 仅支持 Mac 和 Linux 操作系统。但是， Windows 上可以使用 <a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10" target="_blank" rel="noopener noreferrer">WSL2</a> 来实现支持。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>首先，运行 Composer 将 Envoy 安装到你的项目中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/envoy <span class="token parameter variable">--dev</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>安装 Envoy 之后， Envoy 的可执行文件将出现在项目的 <code v-pre>vendor/bin</code> 目录下：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php vendor/bin/envoy</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="编写任务" tabindex="-1"><a class="header-anchor" href="#编写任务"><span>编写任务</span></a></h2>
<h3 id="定义任务" tabindex="-1"><a class="header-anchor" href="#定义任务"><span>定义任务</span></a></h3>
<p>任务是 Envoy 的基础构建元素。任务定义了你想在远程服务器上当任务被调用时所执行的 Shell 命令。例如，你定义了一个任务：在队列服务器上执行 <code v-pre>php artisan queue:restart</code> 命令。</p>
<p>你所有的 Envoy 任务都应该在项目根目录中的 <code v-pre>Envoy.blade.php</code> 文件中定义。下面是一个帮助你入门的例子：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@servers(['web' =&gt; ['user@192.168.1.1'], 'workers' =&gt; ['user@192.168.1.2']])</span>
<span class="line"></span>
<span class="line">@task('restart-queues', ['on' =&gt; 'workers'])</span>
<span class="line">    cd /home/user/example.com</span>
<span class="line">    php artisan queue:restart</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>正如你所见，在文件顶部定义了一个 <code v-pre>@servers</code> 数组，使你可以通过任务声明的 <code v-pre>on</code> 选项引用这些服务器。<code v-pre>@servers</code> 声明应始终放置在单行中。在你的 <code v-pre>@task</code> 声明中，你应该放置任务被调用执行时你期望在服务器上运行的 Shell 命令。</p>
<h4 id="本地任务" tabindex="-1"><a class="header-anchor" href="#本地任务"><span>本地任务</span></a></h4>
<p>你可以通过将服务器的 IP 地址指定为 <code v-pre>127.0.0.1</code> 来强制脚本在本地运行：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@servers(['localhost' =&gt; '127.0.0.1'])</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="导入-envoy-任务" tabindex="-1"><a class="header-anchor" href="#导入-envoy-任务"><span>导入 Envoy 任务</span></a></h4>
<p>使用 <code v-pre>@import</code> 指令，你可以从其他的 Envoy 文件导入它们的故事与任务并添加到你的文件中。导入文件后，你可以像定义在自己的 Envoy 文件中一样执行其中包含的任务：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@import('vendor/package/Envoy.blade.php')</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="多服务器" tabindex="-1"><a class="header-anchor" href="#多服务器"><span>多服务器</span></a></h3>
<p>Envoy 允许你轻松地在多台服务器上运行任务。 首先，在 <code v-pre>@servers</code> 声明中添加其他服务器。每台服务器都应分配一个唯一的名称。一旦定义了其他服务器，你可以在任务的 <code v-pre>on</code> 数组中列出每个服务器：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@servers(['web-1' =&gt; '192.168.1.1', 'web-2' =&gt; '192.168.1.2'])</span>
<span class="line"></span>
<span class="line">@task('deploy', ['on' =&gt; ['web-1', 'web-2']])</span>
<span class="line">    cd /home/user/example.com</span>
<span class="line">    git pull origin {{ $branch }}</span>
<span class="line">    php artisan migrate --force</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="并行执行" tabindex="-1"><a class="header-anchor" href="#并行执行"><span>并行执行</span></a></h4>
<p>默认情况下，任务将在每个服务器上串行执行。 换句话说，任务将在第一台服务器上完成运行后，再继续在第二台服务器上执行。如果你想在多个服务器上并行运行任务，请在任务声明中添加 <code v-pre>parallel</code> 选项：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@servers(['web-1' =&gt; '192.168.1.1', 'web-2' =&gt; '192.168.1.2'])</span>
<span class="line"></span>
<span class="line">@task('deploy', ['on' =&gt; ['web-1', 'web-2'], 'parallel' =&gt; true])</span>
<span class="line">    cd /home/user/example.com</span>
<span class="line">    git pull origin {{ $branch }}</span>
<span class="line">    php artisan migrate --force</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所见，文件顶部定义了一个 <code v-pre>@server</code> 数组，允许你在任务声明的 <code v-pre>on</code> 选项中引用这些服务器。<code v-pre>@server</code> 声明应始终放在一行中。在你的 <code v-pre>@task</code> 声明中，你应该放置任务被调用执行时你期望在服务器上运行的 Shell 命令。</p>
<h4 id="本地任务-1" tabindex="-1"><a class="header-anchor" href="#本地任务-1"><span>本地任务</span></a></h4>
<p>你可以通过将服务器的 IP 地址指定为 <code v-pre>127.0.0.1</code> 来强制脚本在本地运行：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@servers(['localhost' =&gt; '127.0.0.1'])</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="导入-envoy-任务-1" tabindex="-1"><a class="header-anchor" href="#导入-envoy-任务-1"><span>导入 Envoy 任务</span></a></h4>
<p>使用 <code v-pre>@import</code> 指令，你可以从其他的 Envoy 文件导入它们的故事与任务并添加到你的文件中。文件导入后，你可以执行他们所定义的任务，就像这些任务是在你的 Envoy 文件中被定义的一样：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@import('vendor/package/Envoy.blade.php')</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<p>有时，你可能需要在执行 Envoy 任务之前执行一些 PHP 代码。你可以使用 <code v-pre>@setup</code> 指令来定义一段 PHP 代码块，在任务执行之前执行这段代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token class-name type-declaration">setup</span></span>
<span class="line">    <span class="token variable">$now</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DateTime</span><span class="token punctuation">;</span></span>
<span class="line">@endsetup</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你需要在任务执行之前引入其他的 PHP 文件，你可以在 <code v-pre>Envoy.blade.php</code> 文件的顶部使用 <code v-pre>@include</code> 指令：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@include('vendor/autoload.php')</span>
<span class="line"></span>
<span class="line">@task('restart-queues')</span>
<span class="line">    # ...</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="变量" tabindex="-1"><a class="header-anchor" href="#变量"><span>变量</span></a></h3>
<p>如果需要的话，你可以在调用 Envoy 任务时通过在命令行中指定参数，将参数传递给 Envoy 任务：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php vendor/bin/envoy run deploy <span class="token parameter variable">--branch</span><span class="token operator">=</span>master</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以使用 Blade 的「echo」语法访问传入任务中的命令行参数。你也可以在任务中使用 <code v-pre>if</code> 语句和循环。 例如，在执行 <code v-pre>git pull</code> 命令之前，我们先验证 <code v-pre>$branch</code> 变量是否存在：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@servers(['web' =&gt; ['user@192.168.1.1']])</span>
<span class="line"></span>
<span class="line">@task('deploy', ['on' =&gt; 'web'])</span>
<span class="line">    cd /home/user/example.com</span>
<span class="line"></span>
<span class="line">    @if ($branch)</span>
<span class="line">        git pull origin {{ $branch }}</span>
<span class="line">    @endif</span>
<span class="line"></span>
<span class="line">    php artisan migrate --force</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="故事" tabindex="-1"><a class="header-anchor" href="#故事"><span>故事</span></a></h3>
<p>通过「故事」功能，你可以给一组任务起个名字，以便后续调用。例如，一个 <code v-pre>deploy</code> 故事可以运行 <code v-pre>update-code</code> 和 <code v-pre>install-dependencies</code> 等定义好的任务：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@servers(['web' =&gt; ['user@192.168.1.1']])</span>
<span class="line"></span>
<span class="line">@story('deploy')</span>
<span class="line">    update-code</span>
<span class="line">    install-dependencies</span>
<span class="line">@endstory</span>
<span class="line"></span>
<span class="line">@task('update-code')</span>
<span class="line">    cd /home/user/example.com</span>
<span class="line">    git pull origin master</span>
<span class="line">@endtask</span>
<span class="line"></span>
<span class="line">@task('install-dependencies')</span>
<span class="line">    cd /home/user/example.com</span>
<span class="line">    composer install</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦编写好了故事，你可以像调用任务一样调用它：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php vendor/bin/envoy run deploy</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="任务钩子" tabindex="-1"><a class="header-anchor" href="#任务钩子"><span>任务钩子</span></a></h3>
<p>当任务和故事脚本运行时，会执行很多钩子。Envoy 支持的钩子类型有<code v-pre>@before</code>, <code v-pre>@after</code>, <code v-pre>@error</code>, <code v-pre>@success</code>, and <code v-pre>@finished</code>。 这些钩子中的所有代码都被解释为 PHP 并在本地执行，而不是在你的任务与之交互的远程服务器上执行。</p>
<p>你可以根据需要定义任意数量的这些。这些钩子将按照它们在您的 Envoy 脚本中出现的顺序执行。</p>
<h4 id="before" tabindex="-1"><a class="header-anchor" href="#before"><span><code v-pre>@before</code></span></a></h4>
<p>在每个任务执行之前，Envoy 脚本中注册的所有 <code v-pre>@before</code> 钩子都会执行。 <code v-pre>@before</code> 钩子负责接收将要执行的任务的名称：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@before</span>
<span class="line">    if ($task === 'deploy') {</span>
<span class="line">        // ...</span>
<span class="line">    }</span>
<span class="line">@endbefore</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="after" tabindex="-1"><a class="header-anchor" href="#after"><span><code v-pre>@after</code></span></a></h4>
<p>每次任务执行后，Envoy 脚本中注册的所有 <code v-pre>@after</code> 钩子都会执行。 <code v-pre>@after</code> 钩子负责接收已执行任务的名称：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@after</span>
<span class="line">    if ($task === 'deploy') {</span>
<span class="line">        // ...</span>
<span class="line">    }</span>
<span class="line">@endafter</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="error" tabindex="-1"><a class="header-anchor" href="#error"><span><code v-pre>@error</code></span></a></h4>
<p>在每次任务失败后（以大于 <code v-pre>0</code> 的状态码退出执行），Envoy 脚本中注册的所有 <code v-pre>@error</code> 钩子都将执行。 <code v-pre>@error</code> 钩子负责接收已执行任务的名称：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@error</span>
<span class="line">    if ($task === 'deploy') {</span>
<span class="line">        // ...</span>
<span class="line">    }</span>
<span class="line">@enderror</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="success" tabindex="-1"><a class="header-anchor" href="#success"><span><code v-pre>@success</code></span></a></h4>
<p>如果所有任务都已正确执行，则 Envoy 脚本中注册的所有 <code v-pre>@success</code> 钩子都将执行：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@success</span>
<span class="line">    // ...</span>
<span class="line">@endsuccess</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="finished" tabindex="-1"><a class="header-anchor" href="#finished"><span><code v-pre>@finished</code></span></a></h4>
<p>在所有任务都执行完毕后（不管退出状态如何），所有的 <code v-pre>@finished</code> 钩子都会被执行。 <code v-pre>@finished</code> 钩子负责接收已完成任务的状态码，它可能是 <code v-pre>null</code> 或大于或等于 <code v-pre>0</code> 的 <code v-pre>integer</code>：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@finished</span>
<span class="line">    if ($exitCode &gt; 0) {</span>
<span class="line">        // There were errors in one of the tasks...</span>
<span class="line">    }</span>
<span class="line">@endfinished</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="钩子" tabindex="-1"><a class="header-anchor" href="#钩子"><span>钩子</span></a></h3>
<p>当任务和故事运行时，会执行许多钩子。 Envoy 支持的钩子类型有 <code v-pre>@before</code>、<code v-pre>@after</code>、<code v-pre>@error</code>、<code v-pre>@success</code> 和 <code v-pre>@finished</code>。这些钩子中的所有代码都被解释为 PHP 并在本地执行，而不是在与你的任务交互的远程服务器上执行。</p>
<p>你可以根据需要定义任意数量的钩子。 它们将按照它们在你的 Envoy 脚本中出现的顺序执行。</p>
<h4 id="before-1" tabindex="-1"><a class="header-anchor" href="#before-1"><span><code v-pre>@before</code></span></a></h4>
<p>在每个任务执行之前，将执行在你的 Envoy 脚本中注册的所有 <code v-pre>@before</code> 钩子。 <code v-pre>@before</code> 钩子接收将要执行的任务的名称：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@before</span>
<span class="line">    if ($task === 'deploy') {</span>
<span class="line">        // ...</span>
<span class="line">    }</span>
<span class="line">@endbefore</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="after-1" tabindex="-1"><a class="header-anchor" href="#after-1"><span><code v-pre>@after</code></span></a></h4>
<p>每次任务执行后，将执行在你的 Envoy 脚本中注册的所有 <code v-pre>@after</code> 钩子。 <code v-pre>@after</code> 钩子接收已执行任务的名称：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@after</span>
<span class="line">    if ($task === 'deploy') {</span>
<span class="line">        // ...</span>
<span class="line">    }</span>
<span class="line">@endafter</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="error-1" tabindex="-1"><a class="header-anchor" href="#error-1"><span><code v-pre>@error</code></span></a></h4>
<p>在每个任务失败后（退出时的状态大于 <code v-pre>0</code>），在你的 Envoy 脚本中注册的所有 <code v-pre>@error</code> 钩子都将执行。 <code v-pre>@error</code> 钩子接收已执行任务的名称：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@error</span>
<span class="line">    if ($task === 'deploy') {</span>
<span class="line">        // ...</span>
<span class="line">    }</span>
<span class="line">@enderror</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="success-1" tabindex="-1"><a class="header-anchor" href="#success-1"><span><code v-pre>@success</code></span></a></h4>
<p>如果所有任务都没有出现错误，那么在你的 Envoy 脚本中注册的所有 <code v-pre>@success</code> 钩子都将执行：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@success</span>
<span class="line">    // ...</span>
<span class="line">@endsuccess</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="finished-1" tabindex="-1"><a class="header-anchor" href="#finished-1"><span><code v-pre>@finished</code></span></a></h4>
<p>在执行完所有任务后（无论退出状态如何），所有的 <code v-pre>@finished</code> 钩子都将被执行。 <code v-pre>@finished</code> 钩子接收已完成任务的状态代码，它可以是 <code v-pre>null</code> 或大于或等于 <code v-pre>0</code> 的 <code v-pre>integer</code>：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@finished</span>
<span class="line">    if ($exitCode &gt; 0) {</span>
<span class="line">        // There were errors in one of the tasks...</span>
<span class="line">    }</span>
<span class="line">@endfinished</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="运行任务" tabindex="-1"><a class="header-anchor" href="#运行任务"><span>运行任务</span></a></h2>
<p>要运行在应用程序的 <code v-pre>Envoy.blade.php</code> 文件中定义的任务或 story，请执行 Envoy 的 <code v-pre>run</code> 命令，传递你想要执行的任务或 story 的名称。Envoy 将执行该任务，并在任务运行时显示来自远程服务器的输出：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php vendor/bin/envoy run deploy</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="确认任务执行" tabindex="-1"><a class="header-anchor" href="#确认任务执行"><span>确认任务执行</span></a></h3>
<p>如果你想在在服务器上运行给定任务之前进行确认，请将 <code v-pre>confirm</code> 指令添加到您的任务声明中。此选项特别适用于破坏性操作：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@task('deploy', ['on' =&gt; 'web', 'confirm' =&gt; true])</span>
<span class="line">    cd /home/user/example.com</span>
<span class="line">    git pull origin {{ $branch }}</span>
<span class="line">    php artisan migrate</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="通知" tabindex="-1"><a class="header-anchor" href="#通知"><span>通知</span></a></h2>
<h3 id="slack" tabindex="-1"><a class="header-anchor" href="#slack"><span>Slack</span></a></h3>
<p>Envoy 支持在每次任务执行后向 <a href="https://slack.com/" target="_blank" rel="noopener noreferrer">Slack</a> 发送通知。<code v-pre>@slack</code> 指令接受一个 Slack 钩子 URL 和一个频道/用户名称。您可以通过在 Slack 控制面板中创建 「Incoming WebHooks」 集成来检索您的 webhook URL。</p>
<p>你应该将整个 webhook URL 作为传递给 <code v-pre>@slack</code> 指令的第一个参数。<code v-pre>@slack</code> 指令给出的第二个参数应该是频道名称 (<code v-pre>#channel</code>) 或用户名称 (<code v-pre>@user</code>)：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@finished</span>
<span class="line">    @slack('webhook-url', '#bots')</span>
<span class="line">@endfinished</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，Envoy 通知将向通知频道发送一条消息，描述已执行的任务。但是，你可以通过将第三个参数传递给 <code v-pre>@slack</code> 指令来覆盖此消息，以自定义自己的消息：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@finished</span>
<span class="line">    @slack('webhook-url', '#bots', 'Hello, Slack.')</span>
<span class="line">@endfinished</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="discord" tabindex="-1"><a class="header-anchor" href="#discord"><span>Discord</span></a></h3>
<p>Envoy 还支持在每个任务执行后向 <a href="https://discord.com/" target="_blank" rel="noopener noreferrer">Discord</a> 发送通知。<code v-pre>@discord</code> 指令接受一个 Discord Webhook URL 和一条消息。您可以在服务器设置中创建「Webhook」，并选择 Webhook 应该发布到哪个频道来检索 Webhook URL。您应该将整个 Webhook URL 传递到 <code v-pre>@discord</code> 指令中：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@finished</span>
<span class="line">    @discord('discord-webhook-url')</span>
<span class="line">@endfinished</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="telegram" tabindex="-1"><a class="header-anchor" href="#telegram"><span>Telegram</span></a></h3>
<p>Envoy 还支持在每个任务执行后向 <a href="https://telegram.org/" target="_blank" rel="noopener noreferrer">Telegram</a> 发送通知。<code v-pre>@telegram</code> 指令接受一个 Telegram Bot ID 和一个 Chat ID。你可以使用 <a href="https://t.me/botfather" target="_blank" rel="noopener noreferrer">BotFather</a> 创建一个新的机器人来检索Bot ID。你可以使用 <a href="https://t.me/username_to_id_bot" target="_blank" rel="noopener noreferrer">@username_to_id_bot</a> 检索有效的 Chat ID。你应该将整个Bot ID和Chat ID传递到 <code v-pre>@telegram</code> 指令中：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@finished</span>
<span class="line">    @telegram('bot-id','chat-id')</span>
<span class="line">@endfinished</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="microsoft-teams" tabindex="-1"><a class="header-anchor" href="#microsoft-teams"><span>Microsoft Teams</span></a></h3>
<p>Envoy 还支持在每个任务执行后向 <a href="https://www.microsoft.com/en-us/microsoft-teams" target="_blank" rel="noopener noreferrer">Microsoft Teams</a> 发送通知。<code v-pre>@microsoftTeams</code> 指令接受Teams Webhook（必填）、消息、主题颜色（成功、信息、警告、错误）和一组选项。你可以通过创建新的 <a href="https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook" target="_blank" rel="noopener noreferrer">incoming webhook</a> 来检索Teams Webhook。Teams API 有许多其他属性可以自定义消息框，例如标题、摘要和部分。你可以在 <a href="https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL#example-of-connector-message" target="_blank" rel="noopener noreferrer">Microsoft Teams文档</a> 中找到更多信息。你应该将整个Webhook URL 传递到 <code v-pre>@microsoftTeams</code> 指令中：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@finished</span>
<span class="line">    @microsoftTeams('webhook-url')</span>
<span class="line">@endfinished</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/envoy/14903" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/en...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/envoy/14903" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/en...</a></p>
</blockquote>
</div></template>


